package com.e9ab98e991ab.moduler.ui.toast

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.Typeface.BOLD_ITALIC
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.StyleSpan

import com.e9ab98e991ab.libcommon.base.BaseActivity
import com.e9ab98e991ab.libcommon.utils.StatusBarUtil
import com.e9ab98e991ab.libcommon.utils.toast.Toasty
import com.e9ab98e991ab.libcommon.utils.toast.ToastyUtils
import com.e9ab98e991ab.moduler.R
import kotlinx.android.synthetic.main.activity_toast.*

/**
 * @author gaoxin 18-12-20 下午8:08
 * @version V1.0.0
 * @name ToastActivity toast演示
 */
class ToastActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        return R.layout.activity_toast
    }



    override fun initSet() {
        StatusBarUtil.immersiveStatusBar(this)
        StatusBarUtil.setStatusBarLightMode(this)
        StatusBarUtil.setPaddingSmart(this, toast_iv)
    }

    override fun initDate() {

        button_error_toast.setOnClickListener { _ -> ToastyUtils.error( R.string.error_message) }
        button_success_toast.setOnClickListener { _ -> ToastyUtils.success(R.string.success_message) }
        button_info_toast.setOnClickListener { _ -> ToastyUtils.info(R.string.info_message) }
        button_warning_toast.setOnClickListener { _ -> ToastyUtils.warning(R.string.warning_message) }
        button_normal_toast_wo_icon.setOnClickListener { _ -> ToastyUtils.normal( "123") }
        button_normal_toast_w_icon.setOnClickListener { _ ->
            ToastyUtils.normal( R.string.normal_message_with_icon,R.drawable.laptop512)
        }
        button_info_toast_with_formatting.setOnClickListener { _ -> ToastyUtils.info(formattedMessage) }
        button_custom_config.setOnClickListener { _ ->
            Toasty.Config.getInstance()
                    .setTextColor(Color.GREEN)
                    .setToastTypeface(Typeface.createFromAsset(assets, "fonts/PCap Terminal.otf"))
                    .apply()
            ToastyUtils.custom( R.string.custom_message, R.drawable.laptop512,Color.BLACK)

        }
        var falg:Boolean =true ;
        button_config.setOnClickListener{_ ->
            if (falg){
                button_config.text = getString(R.string.system_default_style)
                ToastyUtils.getConfig().setShouldTint(falg)
                falg = false;
            }else{
                button_config.text = getString(R.string.custom_unified_style)
                ToastyUtils.getConfig().setShouldTint(falg)
                falg = true;
            }
        }
    }

    private val formattedMessage: CharSequence
        get() {
            val prefix = "Formatted "
            val highlight = "bold italic"
            val suffix = " text"
            val ssb = SpannableStringBuilder(prefix).append(highlight).append(suffix)
            val prefixLen = prefix.length
            ssb.setSpan(StyleSpan(BOLD_ITALIC),
                    prefixLen, prefixLen + highlight.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            return ssb
        }

}
